//
//  UIImageView+BCWeb.swift
//  BCWebImageKit
//
//  Created by chun.chen on 2021/8/26.
//  UIImageView web图片加载扩展

import BCFoundation
import UIKit

private var originColorKey: Void?

public extension UIImageView {
    /// 自定义 初始化方法
    /// - Parameter name: 图片名称
    convenience init(with name: String) {
        self.init(image: UIImage(named: name))
    }

    /// 原始背景色
    fileprivate(set) var originColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &originColorKey) as? UIColor
        }
        set {
            objc_setAssociatedObject(self, &originColorKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

extension HHWebImage where Base: UIImageView {
    /// 加载url图片
    /// - Parameters:
    ///   - url: 图片url
    ///   - placeholder: 占位图 默认灰色图片
    func setImage(_ url: String?,
                  placeholder: UIImage? = UIImage.bc_image(with: .lightGray)) {
        setImage(url, placeholder: placeholder, complete: nil)
    }

    /// 加载url图片
    /// - Parameters:
    ///   - url: 图片url
    ///   - scaleWidth: 返回对应宽度
    ///   - placeholder: 占位图 默认灰色图片
    func setImage(_ url: String?, scaleWidth: Int,
                  placeholder: UIImage? = UIImage.bc_image(with: .lightGray)) {
        guard let url_ = url else {
            return
        }
        let urlWidth = ImgWidthURL(url_, width: scaleWidth)
        setImage(urlWidth, placeholder: placeholder, complete: nil)
    }

    /// 加载url图片
    /// - Parameters:
    ///   - url: 图片url
    ///   - placeholder: 占位图
    ///   - complete: call back
    func setImage(_ url: String?, placeholder: UIImage?, complete: ((Bool, UIImage?) -> Void)?) {
        let orginalBgColor = base.originColor ?? base.backgroundColor
        if let _ = base.hh.placeholderColor {
            base.originColor = orginalBgColor
            base.backgroundColor = base.hh.placeholderColor
        }
        guard let url_ = url else {
            print("url 不能为空")
            return
        }
        let imgUrl = URL.bc_url(url_)
        base.sd_setImage(with: imgUrl, placeholderImage: placeholder) { [weak base] image, error, _, imageURL in
            guard let strongBase = base, imageURL == imgUrl else {
                complete?(false, image)
                return
            }
            strongBase.backgroundColor = orginalBgColor
            complete?(error == nil, image)
        }
    }

    /// 加载url图片 返回显示对应的圆角图片
    /// - Parameters:
    ///   - url: 图片url
    ///   - radius: 圆角
    ///   - placeholder: 占位图
    func setImage(_ url: String?, radius: Float,
                  placeholder: UIImage? = UIImage.bc_image(with: .lightGray)) {
        guard let url_ = url else {
            print("url 不能为空")
            return
        }
        let imgUrl = URL.bc_url(url_)
        let cacheKey = SDWebImageManager.shared.cacheKey(for: imgUrl)

        if let cacheImg = SDImageCache.shared.imageFromCache(forKey: cacheKey) {
            base.image = cacheImg
            return
        }
        let orginalBgColor = base.originColor ?? base.backgroundColor
        if let _ = base.hh.placeholderColor {
            base.originColor = orginalBgColor
            base.backgroundColor = base.hh.placeholderColor
        }
        base.sd_setImage(with: imgUrl, placeholderImage: placeholder) { [weak base] image, _, _, _ in
            guard let strongBase = base, let image_ = image else {
                return
            }
            strongBase.backgroundColor = orginalBgColor
            let radiusImg = image_.bc_image(withCornerRadius: CGFloat(radius))
            strongBase.image = radiusImg
            SDImageCache.shared.store(radiusImg, forKey: cacheKey) {
            }
        }
    }
}
